#include "logicmanageradaptor.h"
#include "dataDefination.h"
const static QString CONVERSATION_TEMPLATE = "<p><b>%1:</b><br/>%2</p>";
LogicManagerAdaptor::LogicManagerAdaptor(QObject *parent) :
    QObject(parent)
{
    m_userInfoModel = new UserInfoModel(this);
    m_logicManager = new LogicManager(this);
    setupAdaptor();
}

void LogicManagerAdaptor::onMessageUpdated(const QString& ip, const QList<recordInfo>& list)
{
    QString text="";
    for(int i=0;i<list.size();++i)
    {
        text.append(CONVERSATION_TEMPLATE.arg(list[i].getName(), list[i].getContent()));
    }
    QString name=m_userInfoModel->ipToName(ip);
    if(!name.isEmpty())
        emit messageUpdated(ip,name,text);
}

void LogicManagerAdaptor::onContactListUpdated(const QList<userInfo>& list)
{
    m_userInfoModel->clear();
    m_userInfoModel->copyFromList(list);
}
void LogicManagerAdaptor::setupAdaptor()
{
        connect(m_logicManager, SIGNAL(userListChanged(const QList<userInfo> &)),
                          SLOT(onContactListUpdated(const QList<userInfo>&)));
        connect(m_logicManager, SIGNAL(messageUpdate(const QString &, const QList<recordInfo> &)),
                          SLOT(onMessageUpdated(const QString&, const QList<recordInfo>&)));
}
